package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.SortDao;
import com.hnkywl.sts.entity.interfaces.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-17
 * Time: 下午2:54
 * To change this template use File | Settings | File Templates.
 */
@Service
public class SortService {
    private SortDao sortDao;

    @Transactional
    public void checkSerialNumber(String serialNumber, String methodName) throws RuntimeException {
        if (serialNumber == null) {
            throw new RuntimeException("传入serialNumber为空");
        } else {
            List<Sort> sortLists = sortDao.findByMethodName(methodName);
            if (sortLists != null && sortLists.size() != 0) {
                if (serialNumber.compareToIgnoreCase(sortLists.get(0).getSerialnumber()) > 0) {
                    sortDao.updateSort(serialNumber, sortLists);
                } else {
                    throw new RuntimeException("相同指令已调用，无需重复操作！");
                }
            } else {
                sortDao.insertSort(serialNumber, methodName);
            }
        }
    }

    @Autowired
    public void setSortDao(SortDao sortDao) {
        this.sortDao = sortDao;
    }
}
